package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.common.BeanCopyUtils;
import com.woniu.car.domain.TExamine;
import com.woniu.car.dto.ExamineResultDto;
import com.woniu.car.dto.ExamineUpdateDto;
import com.woniu.car.exception.AccountException;
import com.woniu.car.exception.ParamException;
import com.woniu.car.mapper.TExamineMapper;
import com.woniu.car.param.QueryExamineResultParam;
import com.woniu.car.service.TExamineService;
import com.woniu.car.util.AccountResult;
import com.woniu.car.util.AccountStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WWX
 * @since 2021-06-19
 */
@Service
public class TExamineServiceImpl extends ServiceImpl<TExamineMapper, TExamine> implements TExamineService {
    /**
     *@Author 王维鑫
     *@Description 新增审核信息
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public TExamine insertExamine(TExamine tExamine)  {
        tExamine.setExamineStatus(AccountStatus.FORZEN);
        long time = new Date().getTime();
        tExamine.setApplyTime(time);
        boolean save = this.save(tExamine);
        if(save==false){
            throw new AccountException("新增审核信息失败");
        }
        return tExamine;
    }
    /**
     *@Author 王维鑫
     *@Description 修改审核信息
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public ExamineUpdateDto updateExamine(TExamine tExamine)  {
        boolean b = this.updateById(tExamine);
        if(b==false){
            throw new AccountException("修改审核信息失败");
        }
        ExamineUpdateDto examineUpdateDto = new ExamineUpdateDto();
        BeanCopyUtils.copyProperties(tExamine,examineUpdateDto);
        return examineUpdateDto;
    }
    /**
     *@Author 王维鑫
     *@Description 查看审核信息列表
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public List<TExamine> showExamine() {
        List<TExamine> list = this.list();
        return list;
    }
    /**
     *@Author 王维鑫
     *@Description 查询审核信息
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public List<TExamine> queryExamine(TExamine tExamine) {
        QueryWrapper<TExamine> wrapper = new QueryWrapper<>();
        if(null!=tExamine.getExamineId()){
            wrapper.eq("examine_id",tExamine.getExamineId());
        }
        if(null!=tExamine.getBackstageId()){
            wrapper.eq("backstage_id",tExamine.getBackstageId());
        }
        if(null!=tExamine.getExamineResult()){
            wrapper.eq("examine_result",tExamine.getExamineResult());
        }
        if(null!=tExamine.getExamineStatus()){
            wrapper.eq("examine_status",tExamine.getExamineStatus());
        }
        if(null!=tExamine.getShopTel()){
            wrapper.eq("shop_tel",tExamine.getShopTel());
        }
        if(null!=tExamine.getShopId()){
            wrapper.eq("shop_id",tExamine.getShopId());
        }
        List<TExamine> list = this.list(wrapper);
        return list;
    }

    @Override
    @Transactional
    public ExamineResultDto queryExamineResult(QueryExamineResultParam param) throws ParamException {
        TExamine tExamine = new TExamine();
        tExamine.setShopId(param.getShopId());
        List<TExamine> tExamines = this.queryExamine(tExamine);
        if(tExamines.size()==0){
            throw new ParamException("该店铺未查询到审核信息");
        }else {
            ExamineResultDto examineResultDto = new ExamineResultDto();
            TExamine tExamine1 = tExamines.get(0);
            if(tExamine1.getExamineResult()== AccountResult.PASS){
                examineResultDto.setResult("审核通过");
            }
            if(tExamine1.getExamineResult()==AccountResult.NO){
                examineResultDto.setResult("审核未通过");
            }
            BeanCopyUtils.copyProperties(tExamine1,examineResultDto);
            return examineResultDto;
        }
    }
}
